# 内置的过滤，用于添加条件
from rest_framework.filters import BaseFilterBackend


class MinFilterBackend(BaseFilterBackend):
    """
    前端传:min_id/max_id
    """

    def filter_queryset(self, request, queryset, view):
        """用于min_id进行处理"""
        # 给视图类的queryset，添加新的条件
        # queryset=models.Release.objects.filter(is_show=True, is_delete=False).order_by('-id')
        min_id = request.query_params.get('min_id')
        # print(min_id)
        if min_id:
            return queryset.filter(id__lt=min_id).order_by('-id')
        return queryset


class MaxFilterBackend(BaseFilterBackend):
    """对应max_id进行处理"""

    def filter_queryset(self, request, queryset, view):
        max_id = request.query_params.get('max_id')
        if max_id:
            return queryset.filter(id__gt=max_id).order_by('id')
        return queryset
